<?php


namespace Beerus\Modules\DecoCompany\Controllers;


use App\Pockets\GridDataModel as pGridDataModel;
use Beerus\Modules\DecoCompany\Actions\ServiceRemark;
use Beerus\Modules\DecoCompany\Actions\ServiceFail;
use Beerus\Modules\DecoCompany\Actions\ServicePass;
use Doraemon\model\DecoCompanyServiceContent;
use Doraemon\model\DesignerOperatorBind\DesignerOperatorBind;
use Doraemon\model\DesignerService\DesignerServiceSpot;
use Doraemon\model\DesignerService\ServiceContentRecord;
use Doraemon\model\Member;
use Doraemon\service\DesignerService\DesignerService as sDesignerService;
use Doraemon\service\DesignerService\ServiceSpotService;
use Doraemon\tools\Hhzurl;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Displayers\DropdownActions;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;

class DecoServiceController
{
    const PAGE_SIZE = 20;

    public function list(Request $request, Content $content)
    {
        $params = $request->all();

        $page_size = $request->input('per_page', self::PAGE_SIZE);
        $page = $request->input('page', 1);

        $condition = [
            'data_status' => DecoCompanyServiceContent::STATUS_NORMAL
        ];
        if (!empty($params['uid'])) {
            $condition['uid'] = $params['uid'];
        }
        if (!empty($params['nick'])) {
            $uid = Member::get('uid', ['nick' => $params['nick']]);
            if ($uid) {
                $condition['uid'] = $uid;
            }
        }
        if (!empty($params['check_status'])) {
            $condition['check_status'] = $params['check_status'];
        }

        $total = DecoCompanyServiceContent::count($condition);

        $condition['ORDER'] = [
            'id' => 'DESC'
        ];
        $condition['LIMIT'] = [
            ($page - 1) * $page_size,
            $page_size
        ];

        $lists = DecoCompanyServiceContent::select('*', $condition);
        $this->formatList($lists);

        $gridModel = new pGridDataModel($lists, $total, [
            'perPage' => $page_size
        ]);

        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->setActionClass(DropdownActions::class);
                $grid->paginate($gridModel->getPerPage());
                $grid->perPages([10, 20, 30, 50, 100, 500]);
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableCreateButton();

                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();
                    $filter->column(1 / 4, function ($filter) {
                        $filter->equal('uid', 'UID')->placeholder('请输入设计师UID');
                    });
                    $filter->column(1 / 3, function ($filter) {
                        $filter->equal('nick', '昵称')->placeholder('请输入设计师昵称');
                    });
                    $filter->column(1 / 4, function ($filter) {
                        $filter->equal('check_status', '审核状态')->select(DecoCompanyServiceContent::AUDIT_TEXT);
                    });
                    $filter->expand();
                });

                $grid->column('id', 'ID')->width(60);
                $grid->column('uid', 'UID')->width(80);
                $grid->column('nick', '昵称')->width(100);
                $grid->column('edit_time', '发布时间')->display(function () {
                    $link = Hhzurl::getDomain() . "/decoration-company/service-info?source=1&uid={$this->uid}";
                    return '<a class="fa " href="javascript:layer.open({
					    type: 2,
	                    title: \'预览\', 
	                    maxmin: true, 
	                    area: [\'600px\', \'800px\'],
	                    fixed: false,
	                    offset: \'100px\',
	                    content: \'' . $link . '\' });">' . htmlentities($this->edit_time) . '</a>';
                });
                $grid->column('check_status', '审核状态')->using([
                    1 => '待审核',
                    2 => '审核通过',
                    3 => '审核不通过',
                ], '未知')->dot([
                    1 => 'info',
                    2 => 'success',
                    3 => 'danger',
                ], 'warning');
                $grid->column('remark', '备注');
                $grid->column('reason', '审核理由');
                $grid->column('check_time', '审核时间')->display(function () {
                    return $this->check_time > 0 ? date("Y-m-d H:i:s", $this->check_time) : '-';
                });
                $grid->column('admin_name', '最后审核人');
                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->disableDelete();
                    $actions->disableEdit();
                    $actions->disableView();

                    if ($actions->row->check_status == DecoCompanyServiceContent::CHECK_PENDING) {
                        $actions->add(new ServicePass);
                        $actions->add(new ServiceFail);
                    }
                    $actions->add(new ServiceRemark);
                });
            }
        );

        $tab = new Tab();
        $tab->add('服务指南审核列表', $gridList->render());
        $content->row($tab);
        return $content
            ->header('服务指南审核')
            ->description(' ')
            ->breadcrumb(['text' => '装修公司管理']);
    }

    public function formatList(&$list)
    {
        foreach ($list as &$item) {
            $item['nick'] = Member::get('nick', ['uid' => $item['uid']]);
            $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
            $item['edit_time'] = empty($item['edit_time']) ? '-' : date('Y-m-d H:i:s', $item['edit_time']);
            $item['admin_name'] = empty($item['admin_name']) ? '-' : $item['admin_name'];
        }
        unset($item);
    }
}
